classdef MyRoi< handle
    properties (Constant)
        
    end
    properties
        points;
        regionNo;
        stats;
        bands;
        hist;
    end
    
    methods(Static = true)
        %% 批量打开文件
        function roies = LoadBatchRoies(fileName, bands, isRefl)
            fid = fopen(fileName, 'r');
            fgetl(fid); %%提取第一行没用的
            
            roies = {};
            
            while true
                roi = MyRoi(bands);
                roi.loadDataForFid(fid);
                if isempty(roi.stats)
                    break
                end
                if isRefl == true
                    roi.stats(:, 2:end) = roi.stats(:, 2:end) / 10000;
                end
                
                %% check
                roi.checkData(size(roies, 1) + 1);
                %%
                
                if isempty(roies)
                    roies = cell(1,1);                    
                    roies{1} = roi;
                    
                    ins = zeros(1, 1);
                    ins(1) = roi.regionNo;
                else
                    roies{end+1, 1} = roi;
                    
                    ins(end + 1) = roi.regionNo;
                end
            end
            [~, INS] = sort(ins);
            roies = roies(INS);%%增加了顺序处理，ROI在ENVI中的顺序可能不一样
            fclose(fid);
        end
        
        
        function SaveAllRoi(roies, fileRoiPath, roiRange, suffix)
            for i = 1 : length(roies)
                fileName = fullfile(fileRoiPath, [num2str(roiRange(i)), suffix, '.mat']);
                
                roi = roies{i};
                %                 roi.saveMatFile(self, fileRoi, fileStat)
                save(fileName, 'roi')
                
            end
        end
        
        function [xx, bands] = RemoveBands(xx, first, last, bb)
            for i = 1 : length(xx)
                data = xx{i};
                xx{i} = data(first+1 : end - last, :);
            end
            if nargin >3
                bands = bb(first+1 : end - last);
            else
                bands = [];
            end
        end
 
    end
    
    
    
    methods
        
        function self = MyRoi(bands) %返回值必须是一个obj
            self.bands = bands;
            self.hist = cell(bands, 1);
        end
        
        %%根据打开文件读取
        function loadDataForFid(self, fid)
            %% %%提取第2行：ROI: Region #2 [Red] 1204 points
            line = fgetl(fid); %%提取没用的
            if isnumeric(line) &&  line == -1
                return
            end
            
            code = textscan(line, '%[^:] %s %s #%d %s %d %s', 1);
            
            self.points = code{6};  %% int 32 cast(code{5}, 'double')
            self.regionNo = code{4};
            
            %%空余行
            line = fgetl(fid); %%提取没用的
            if isempty(line)
                line = fgetl(fid); %%提取没用的
            end
            
            %% 提取 每个band的统计信息
            %             lines = textscan(fid, '%s %d %d %d %f %f', self.bands);fgetl(fid);
            lines = textscan(fid, '%s %d %f %f %f %f', self.bands);fgetl(fid);
            self.stats = [double(lines{2}), double(lines{3}), double(lines{4}), lines{5}, lines{6}];
            
            %%提取 每个band的直方图
            line = fgetl(fid); %%空余行
            self.extractHists(fid);%%
        end
        
        %% 只打开一个文件
        function loadOneFile(self, fileName)
            fid = fopen(fileName, 'r');
            fgetl(fid); %%提取第一行没用的
            
            %% %%提取第2行：ROI: Region #2 [Red] 1204 points
            line = fgetl(fid); %%提取没用的
            code = textscan(line, '%s %s #%d %s %d %s', 1);
            
            self.points = code{5};  %% int 32 cast(code{5}, 'double')
            self.regionNo = code{3};
            
            %%空余行
            line = fgetl(fid); %%提取没用的
            line = fgetl(fid); %%提取没用的
            
            %% 提取 每个band的统计信息
            %             lines = textscan(fid, '%s %d %d %d %f %f', self.bands);fgetl(fid);
            lines = textscan(fid, '%s %d %f %f %f %f', self.bands);fgetl(fid);
            self.stats = [double(lines{2}), double(lines{3}), double(lines{4}), lines{5}, lines{6}];
            
            %%提取 每个band的直方图
            line = fgetl(fid); %%空余行
            self.extractHists(fid);%%
            
            fclose(fid);
        end
        
        %% 提取 每个band的直方图
        function extractHists(self, fid)
            for i = 1 : self.bands
                line = fgetl(fid); %%提取没用的
                
                oneHist = zeros(0, 5);
                kk = 0;
                while (true)
                    line = fgetl(fid); %%提取1行
                    if isempty(line) || (isnumeric(line) &&  line == -1)
                        self.hist{i} = oneHist;
                        break;
                    end
                    
                    %                     data = textscan(line, '%s %d %d %d %f %f', 'delimiter','\t');
                    data = textscan(line, '%s %f %d %d %f %f', 'delimiter','\t');
                    oneHist(end+1, :) = [double(data{2}), double(data{3}), ...
                        double(data{4}), data{5}, data{6}];
                end
                
                % % %                 line = fgetl(fid); %%提取没用的
                % % %
                % % %                 lines = textscan(fid, '%s %d %d %d %f %f', 'delimiter','\t');
                % % %                 self.hist{i} = [double(lines{2}), double(lines{3}), double(lines{4}), ...
                % % %                     lines{5}, lines{6}];
                % % %
                % % %                 if isnan(lines{end}(end))
                % % %                     self.hist{i} = self.hist{i}(1:end-1, :);
                % % %                     disp(['extractHists__bank:', num2str(i)]);
                % % %                 end
            end
            if isnan(self.hist{end}(end))
                disp('error in  extractHists');
            end
        end
        
        %% 检查数据是否一致
        function checkData(self, k)
            for i = 1 : self.bands
                lower = self.stats(i, 2);
                higher = self.stats(i, 3);
                if lower == self.hist{i}(1,1) && higher == self.hist{i}(end,1)
                    continue;
                end
                disp(['不一致:', num2str(k), '__', num2str(i)]);
                disp([lower, higher, self.hist{i}(1,1), self.hist{i}(end,1)])
            end
        end
        
        %% 保存数据
        function saveMatFile(self, fileRoi, fileStat)
            save(fileRoi, self)
            save(fileStat, self.stats, self.points, self.bands);
        end
        
        
    end
    
    
end